##parameters=modelIdentities, componentMapping, repositoryIdentities=None

ass = context.knowledgeengine_assessmentstorageservice

allAssessments = []

for modelIdentity in modelIdentities:
    model = context.getComponentByIdentity( modelIdentity )
        
    allAssessments.extend( ass.getAssessmentsForModel( model ) )
    


fields = ["assessmentIdentity"]
titles = {"assessmentIdentity":"Assessment Identity"}

for componentIdentity, title in componentMapping:
    fields.append( componentIdentity )
    titles[ componentIdentity ] = title

brains = []

for assessment in allAssessments:
    
    # If a repository filter is specified, only include assessments that are in those repositories
    if repositoryIdentities:
        keep = False # By default, mark as "Not Keeping"
        assessmentRepositoryIdentities = assessment.getRepositoryIdentities()   # Get the repositories the assessment si in
        for repositoryIdentity in repositoryIdentities:                         # For each repository in the filter
            if repositoryIdentity in assessmentRepositoryIdentities:            # Cross reference against the assessment repositories
                keep = True                                                     # If there is any crossover between the filter and assessment repositories, mark this assessment as "Keep"
                break
            
        if not keep: # This assessment is not in the filtered repositories
            continue
                                            

    brain = { "assessmentIdentity": assessment.getIdentity() }
    for componentIdentity, title in componentMapping:                
        brain[ componentIdentity ] = assessment.getComponentByIdentityValue( componentIdentity )
        
    brains.append( brain )
    


brains.insert( 0, titles )

from Products.KnowledgeEngine.Utils.helpers import renderCSV

csv = renderCSV( fields, brains )
        


return csv
    